package com.liuxinlong.modules.log.service.impl;

import com.liuxinlong.common.ExcelData;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.modules.dao.UserDao;
import com.liuxinlong.modules.dao.UserLogDao;
import com.liuxinlong.modules.entity.UserLog;
import com.liuxinlong.modules.log.service.UserLogService;
import com.liuxinlong.utils.ExcelUtils;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用户日志实现类
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2021-12-28
 *
 */
@Service
@Slf4j
public class UserLogServiceImpl implements UserLogService {
	@Autowired
	private UserLogDao userLogDao;

	@Autowired
	private UserDao userDao;

	@Override
	public List<Map<String, Object>> pageUserLog(Map<String, Object> queryParam) {
		List<UserLog> originList = userLogDao.pageUserLogList(queryParam);
		List<Map<String, Object>> resultList = completeResult(originList, (int) queryParam.get("startNum"));
		return resultList;
	}

	@Override
	public int getUserLogCount(Map<String, Object> queryParam) {
		return userLogDao.getUserLogCount(queryParam);
	}

	@Override
	public void addUserLog(UserLog userLog) {
		userLog.setId(SnowFlake.nextIdStr());
		userLogDao.insert(userLog);
	}

	@Override
	public void updateUserLog(UserLog userLog) {
		// TODO Auto-generated method stub
		userLogDao.updateById(userLog);
	}

	@Override
	public void deleteUserLog(String id) {
		// TODO Auto-generated method stub
		userLogDao.deleteById(id);
	}

	@Override
	public void exportUserLog(Map<String, Object> queryParam, HttpServletResponse response) {
		ExcelData data = new ExcelData();
		data.setFileName("用户操作日志导出excel.xlsx");
		queryParam.put("startNum",0);
		queryParam.put("pageSize",10000);
		List<UserLog> originList  = userLogDao.pageUserLogList(queryParam);
		Map<String, Map<String, String>> userMap = userDao.getUserMap();
		String[] head = {"序号", "功能模块", "操作类型", "操作内容", "操作结果","ip", "操作用户", "操作时间"};
		data.setHead(head);
		String[][] dataList = new String[originList.size()][head.length];
		int index = 0;
		for (UserLog item : originList) {
			dataList[index][0] = String.valueOf(index + 1);
			dataList[index][1] = item.getBusiness();
			dataList[index][2] = item.getOperateType();
			dataList[index][3] = item.getContent();
			dataList[index][4] = item.getOperateResult();
			dataList[index][5] = item.getIp();
			dataList[index][6] = userMap.containsKey(item.getOperateUser()) ? userMap.get(item.getOperateUser()).get("name") : "";
			dataList[index][7] = item.getOperateTime();
			index++;
		}
		data.setData(Arrays.asList(dataList));
		ExcelUtils.exportExcel(response, data);
	}

	/**
	 * 数据填充
	 *
	 * @param originList 初始数据列表
	 * @param startNum   查询起始值
	 * @return 完整数据
	 */
	private List<Map<String, Object>> completeResult(List<UserLog> originList, int startNum) {
		List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
		Map<String, Map<String, String>> userMap = userDao.getUserMap();
		int sort = startNum + 1;
		for (UserLog userLog : originList) {
			Map<String, Object> map = ObjectUtils.object2Map(userLog);
			map.put("sort", sort);
			String operateUser = userLog.getOperateUser();
			if (StringUtils.isNotEmpty(operateUser)) {
				map.put("operateUserName", userMap.containsKey(operateUser) ? userMap.get(operateUser).get("name") : operateUser);
			}
			resultList.add(map);
			sort++;
		}
		return resultList;

	}
}
